<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>广告运营管理</el-breadcrumb-item>
        <el-breadcrumb-item>广告配置</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @search-change="searchChange"
        @row-del="rowDel"
      >
        <template slot="category_name_list" slot-scope="scope">
          <el-tag
            v-for="(item, index) in scope.row.category_name_list.split(',')"
            :key="index"
            >{{ item }}</el-tag
          >
        </template>
        <template slot="free_ad_book_ids" slot-scope="scope">
          <router-link
            :to="
              '/org/ad/bookinfo?id=' + scope.row.id + '&name=免广告书籍&type=2'
            "
          >
            <el-button type="primary" size="mini">查看书籍详情</el-button>
          </router-link>
        </template>
        <template slot="add_book_ids" slot-scope="scope">
          <router-link
            :to="
              '/org/ad/bookinfo?id=' + scope.row.id + '&name=补充书籍&type=1'
            "
          >
            <el-button type="primary" size="mini">查看书籍详情</el-button>
          </router-link>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import {
  adShowBook as baseURL,
  selectVersion,
  bookCategory,
} from "@/api/cat/baseUrls";

export default {
  data() {
    return {
      //数据存储区
      page: {
        total: 1,
      },
      form: {},
      type: true,
      data: [],
      option: {
        index: true,
        indexLabel: "序号",
        addBtn: true,
        editBtn: true,
        viewBtn: false,
        delBtn: true,
        searchBtn: true,
        refreshBtn: true,
        excelBtn: true,
        border: true,
        column: [
          {
            label: "版本号",
            prop: "version_name",
            type: "select",
            search: true,
            placeholder: "请输入版本号",
            dicUrl: `${catUrl}/${selectVersion.selectVersion}`,
            props: {
              label: "app_version",
              value: "app_version",
            },
            dicFormatter: (res) => {
              return res.source;
            },
            rules: [
              {
                required: true,
                message: "版本号必须填写",
              },
            ],
          },
          {
            label: "生效分类",
            prop: "category_name_list",
            type: "input",
            width: "350px",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            required: true,
            slot: true,
          },
          {
            label: "生效分类",
            prop: "effect_book_categorys",
            span: 24,
            type: "checkbox",
            border: true,
            hide: true,
            dicData: [],
          },
          {
            label: "免广告书籍id",
            prop: "free_ad_book_ids",
            type: "textarea",
            required: true,
            slot: true,
          },
          {
            label: "补充书籍id",
            prop: "add_book_ids",
            type: "textarea",
            required: true,
            slot: true,
          },
        ],
      },
    };
  },
  methods: {
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];
      delete row["$app_version"];
      row.app_id = "61096114";

      if (row.free_ad_book_ids) {
        row.free_ad_book_ids = row.free_ad_book_ids.split(",");
      }

      if (row.add_book_ids) {
        row.add_book_ids = row.add_book_ids.split(",");
      }

      let data = await common(baseURL.save, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      
      if (row.free_ad_book_ids) {
        row.free_ad_book_ids = row.free_ad_book_ids.split(",");
      }

      if (row.add_book_ids) {
        row.add_book_ids = row.add_book_ids.split(",");
      }

      let data = await common(baseURL.update, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let data = await common(baseURL.delete, { id: row.id }, null, "GET");
        })
        .then(() => {
          this.getList();
        });
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.pageAndParam,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      response.data.forEach(row=>{
      if (row.free_ad_book_ids) {
        row.free_ad_book_ids = row.free_ad_book_ids.join(",");
      }

      if (row.add_book_ids) {
        row.add_book_ids = row.add_book_ids.join(",");
      }
      })
      this.data = response.data;
      this.loading = false;
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    pages(data) {
      this.page.total = data.total;
      this.page.pageSize = data.size;
      this.page.currentPage = data.current;
    },
    searchChange(params, done) {
      this.condition = [];
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
    async created() {
    let data = await common(`${bookCategory.getListByLeave}/1`);
    if (data.code == 1) {
      this.option.column[2].dicData = data.data;
    }
  }
};
</script>